﻿using JT808Protocol.Abstracts;
using JT808Protocol.Models;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Threading.Channels;
using System.Threading.Tasks;

namespace JT808Gateway.Core.Forwarders
{
    /// <summary>
    /// 进程转发器
    /// </summary>
    public class InMemoryForwarder : IDataForwarder
    {
        private Channel<JT808Package> channel = null;

        public InMemoryForwarder(IServiceCollection services)
        {
            channel = Channel.CreateBounded<JT808Package>(new BoundedChannelOptions(120000) { 
                 FullMode = BoundedChannelFullMode.DropOldest
            });

            services.AddSingleton(channel);
        }

        public async Task SendAsync(JT808Package package)
        {
            await channel.Writer.WriteAsync(package);
        }
    }
}